//
//  SegmentDownloadViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2021/2/21.
//  Copyright © 2021 Xing. All rights reserved.
//

#import "SegmentDownloadViewController.h"
#import "LDDownloadManager.h"
#import <Masonry.h>

@interface SegmentDownloadViewController()

@property (strong, nonatomic) UIButton *downloadButton;
@property (strong, nonatomic) UIButton *deleteButton;
@property (strong, nonatomic) UIProgressView *progressView;
@property (copy, nonatomic) NSString *url;

@end

@implementation SegmentDownloadViewController

- (void)loadView {
    
    [super loadView];
    
    self.progressView = [[UIProgressView alloc] init];
    [self.view addSubview:self.progressView];
    
    self.downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.downloadButton addTarget:self action:@selector(button_Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
    [self.view addSubview:self.downloadButton];
    
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteFile:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.deleteButton];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_equalTo(20);
            make.top.mas_equalTo(kNavBarHeight+20);
    }];
    [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(44);
        make.centerX.equalTo(self.view.mas_centerX);
            make.centerY.equalTo(self.progressView.mas_bottom).offset(60);
    }];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(44);
            make.centerX.equalTo(self.view.mas_centerX);
            make.top.equalTo(self.downloadButton.mas_bottom).offset(20);
    }];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.url = @"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg";
    self.progressView.progress = [[LDDownloadManager sharedInstance] progress:self.url];
    [self.downloadButton addTarget:self action:@selector(button_Click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)button_Click:(UIButton *)button{
    
    [[LDDownloadManager sharedInstance] download:self.url progress:^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.progressView.progress = progress;
            NSLog(@"progress==%f",progress);
        });
    } state:^(FileDownloadState state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self refreshButtonWithDownloadState:state];
        });
    } completionHandler:^(NSString * filePath, NSError * error) {
        
    }];
}
- (void)refreshButtonWithDownloadState:(FileDownloadState)state{
    switch (state) {
        case FileDownloadStart:
        {
            [self.downloadButton setTitle:@"暂停" forState:UIControlStateNormal];
            [self.downloadButton setBackgroundColor:RGB_INT(255, 159, 97)];
        }
            break;
        case FileDownloadSuspended:
        {
                [self.downloadButton setTitle:@"继续" forState:UIControlStateNormal];
        }
        case FileDownloadFailed:
        {
            [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
        }
            break;
        case FileDownloadCompleted:{
            [self.downloadButton setTitle:@"已下载" forState:UIControlStateNormal];
            [self.downloadButton setBackgroundColor:RGB_INT(187, 187, 187)];
        }
            break;
        default:
            break;
    }
}
- (void)deleteFile:(id)sender {
    [[LDDownloadManager sharedInstance] deleteFileWithUrl:self.url];
    self.progressView.progress = 0;
    [self refreshButtonWithDownloadState:FileDownloadFailed];
}


@end
